package com.cs307.redcross.client;

import java.io.Serializable;
import java.util.Vector;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.AbsolutePanel;

public class SendSMSContent extends Content implements Serializable {

	private static final long serialVersionUID = 689764351669829075L;
	Vector<Group> groupVector = new Vector<Group>();
	
	private final DatabaseHandlerServiceAsync databaseHandlerService = GWT.create(DatabaseHandlerService.class);
	private final String relogString = "If this problem persists try relogging into the system.";
	
	ListBox groupListbox;
	TextArea message;
	TextBox subject;
	
	public SendSMSContent()
	{
		
		AbsolutePanel absolutePanel = new AbsolutePanel();
		initWidget(absolutePanel);
		absolutePanel.setSize("1040px", "638px");
		
		Label lblSelectAGroup = new Label("Select a group to send your message to:");
		absolutePanel.add(lblSelectAGroup, 10, 10);
		lblSelectAGroup.setSize("239px", "18px");
		
		groupListbox = new ListBox();
		groupListbox.setVisibleItemCount(5);
		groupListbox.setSelectedIndex(-1);
		absolutePanel.add(groupListbox, 10, 34);
		groupListbox.setSize("239px", "78px");
		
		Label lblSubject = new Label("Subject:");
		absolutePanel.add(lblSubject, 10, 118);
		lblSubject.setSize("58px", "18px");
		
		subject = new TextBox();
		absolutePanel.add(subject, 10, 142);
		subject.setSize("231px", "13px");
		
		Label label_2 = new Label("Message: ");
		absolutePanel.add(label_2, 10, 169);
		label_2.setSize("58px", "18px");
		
		message = new TextArea();
		absolutePanel.add(message, 10, 193);
		message.setSize("235px", "217px");
		
		Button button = new Button("Send");
		absolutePanel.add(button, 10, 420);
		button.setSize("91px", "25px");
		button.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                sendMessage();
                subject.setText(null);
            	message.setText(null);
            }
		});
		
		databaseHandlerService.getAllGroups(new AsyncCallback<Vector<Group>>() {
			@Override
			public void onSuccess(Vector<Group> result) {
				groupVector = new Vector<Group>();
				if (result != null && !result.isEmpty()) {
					for (Group group : result) {
						if (group == null)
							continue;
						
						groupVector.add(group);
						groupListbox.addItem(group.getName());
					}
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Error: Failed to contact server. " + relogString);
				return;
			}
		});
	}
	
    private void sendMessage()
    {
            
            if (groupListbox.getSelectedIndex() != -1 && message.getText().length() > 0 && subject.getText().length() > 0)
            {
                    Group selectedGroup = groupVector.get(groupListbox.getSelectedIndex());
                    Vector<Person> people = selectedGroup.getPersonList();
                    String messageText = message.getText();
                    String subjectStr = subject.getText();
                    databaseHandlerService.sendSMS(people, messageText, subjectStr ,new AsyncCallback<Boolean>() {
                            public void onSuccess(Boolean result) {
                                    if (result)
                                    {
                                            Window.alert("Your message was sent successfully!");
                                    }
                                    else
                                    {
                                            Window.alert("Your message was not sent");
                                    }

                            }
                            
                            public void onFailure(Throwable caught) {
                                    Window.alert("Error: Failed to contact server. " + relogString);
                                    return;
                            }
                    });
            }
            else 
            {
                    Window.alert("Your message was not sent because of missing recipients, missing subject or missing message text.");
            }
    }

}
